package com.wsm.common.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wsm.common.annotation.RedisCache;
import com.wsm.common.config.CloseConfig;
import com.wsm.common.domain.CloseDO;
import com.wsm.common.mapper.CloseDao;
import com.wsm.common.service.CloseService;
import com.wsm.common.utils.SnowFlake;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service
public class CloseServiceImpl extends ServiceImpl<CloseDao, CloseDO> implements CloseService {
	@Autowired
	private CloseDao closeDao;
	@Autowired
	CloseConfig closeConfig;

	@Override
	@RedisCache(value = "close")
	public CloseDO get(Long id) {
		return closeDao.get(id);
	}

	@Override
	@RedisCache(value = "close")
	public List<CloseDO> list(Map<String, Object> map) {
		return closeDao.list(map);
	}

	@Override
	@RedisCache(value = "close")
	public int count(Map<String, Object> map) {
		return closeDao.count(map);
	}

	@Override
	@RedisCache(value = "close", flush = true)
	public boolean save(CloseDO close) {
		close.setId(SnowFlake.nextId());
		if (closeDao.save(close) > 0) {
			closeConfig.setMap(close.getModel(), close.getFlag());
			return true;
		}
		return false;
	}

	@Override
	@RedisCache(value = "close", flush = true)
	public boolean update(CloseDO close) {
		if (closeDao.orUpdate(close) > 0) {
			closeConfig.setMap(close.getModel(), close.getFlag());
			return true;
		}
		return false;
	}

	@Override
	@RedisCache(value = "close", flush = true)
	public boolean remove(Long id, String model) {
		if (closeDao.remove(id) > 0) {
			closeConfig.deleteKey(model);
			return true;
		}
		return false;
	}

	@Override
	@RedisCache(value = "close", flush = true)
	public int batchRemove(Long[] ids) {
		return closeDao.batchRemove(ids);
	}

	@Override
	@RedisCache(value = "close", flush = true)
	public boolean changeStatus(Long id, String model, String cmd) {
		CloseDO closeDO = new CloseDO();
		closeDO.setId(id);
		closeDO.setFlag(cmd);
		if (closeDao.orUpdate(closeDO) > 0) {
			closeConfig.setMap(model, cmd);
			return true;
		}
		return false;
	}

}
